﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xiaov.SmartWeb.Infrastructure.ExtMethod;

namespace Xiaov.SmartWeb.Model.Domain
{
    public class ModulePermissionView
    {
        /// <summary>
        /// 模块Id
        /// </summary>
        public string ModuleId { get; set; }

        /// <summary>
        /// 操作代码
        /// </summary>
        public int PermissionKey { get; set; }

        /// <summary>
        /// 权限值
        /// </summary>
        public int PermissionValue { get; set; }


        public static List<ModulePermissionView> GetList(object RequestStr)
        {
            List<ModulePermissionView> list = new List<ModulePermissionView>();
            if (RequestStr!=null&&!string.IsNullOrEmpty(RequestStr.ToString()))
            {
                string[] str = RequestStr.ToString().Split(',');
                foreach (var q in str)
                {
                    ModulePermissionView m = new ModulePermissionView();
                    m.ModuleId = q.Split('$').First();
                    m.PermissionKey = q.Split('$').Last().ToInt32();
                    m.PermissionValue = System.Math.Pow(2, m.PermissionKey).ToInt32();
                    list.Add(m);
                }
                var s = from p in list
                        group p by p.ModuleId into g
                        select new { g.Key, Sum = g.Sum(p => p.PermissionValue) };
                list = new List<ModulePermissionView>();
                foreach (var x in s)
                {                    
                    ModulePermissionView m = new ModulePermissionView();
                    m.ModuleId = x.Key;
                    m.PermissionValue = x.Sum;
                    list.Add(m);
                }

            } return list;
        }
    }
}
